<?php
declare (strict_types = 1);

namespace app\controller;

use app\model\DealModel;
use app\model\IdleModel;
use app\model\UserModel;
use think\facade\Db;
use think\Request;

class Deal extends Base
{

//    创建交易
    public function addDeal(Request $request){

        $buy_user_id=UserModel::getUseridByToken($request->param('token')); //获取买家用户id
        $sale_user_id=$request->param('sale_user_id');   //卖家用户id
        $idle_item_id=$request->param('idle_item_id');

        $deal_to_user_remark=$request->param('deal_to_user_remark');
        $idle=IdleModel::find($idle_item_id);

        if($idle==null){
            return json(['code' => 0]);
        }


        $deal=new DealModel();
        $data=['deal_idle_item_id'=>$idle_item_id,
            'deal_from_user_id'=>$sale_user_id,
            'deal_to_user_id'=>$buy_user_id,
            'deal_to_user_remark'=>$deal_to_user_remark];

        $data_idle=['idle_item_ifdeal'=> 1];

        if($deal->save($data)){
            if($idle->save($data_idle)){
                return json(['code' => 1]);
            }
        }

        return json(['code' => 0]);
    }


//    获取我发起的交易 == 我要买的数据
    public function getMyBuyDeal(Request $request){

        $user_id=UserModel::getUseridByToken($request->param('token')); //获取用户id

        $deal=new DealModel();

        $page= $request->param('page');
        $limit= $request->param('limit');

        $info=$deal->table('deal a,user b,idle_item c')
                   ->where('deal_to_user_id',$user_id)
                   ->where('a.deal_from_user_id = b.user_id and a.deal_idle_item_id = c.idle_item_id')
                   ->page($page)->limit($limit)->select();

        $total=$deal->where('deal_to_user_id',$user_id)->count('deal_id');


        return json(['code'=>1,'info'=>$info,'total'=>$total]);

    }

//    获取我收到的交易 == 要买我的闲置物品的
    public function getMySaleDeal(Request $request){

        $user_id=UserModel::getUseridByToken($request->param('token')); //获取卖家用户id

        $deal=new DealModel();

        $page= $request->param('page');
        $limit= $request->param('limit');

        $info=$deal->table('deal a,user b,idle_item c')
            ->where('a.deal_from_user_id',$user_id)
            ->where('a.deal_to_user_id = b.user_id and a.deal_idle_item_id = c.idle_item_id')
            ->page($page)->limit($limit)->select();

        $total=$deal->where('deal_from_user_id',$user_id)->count('deal_id');


        return json(['code'=>1,'info'=>$info,'total'=>$total]);

    }

//    获取所有交易信息
    public function getAllDeals(Request $request){

        $deal=new DealModel();

        $page= $request->param('page');
        $limit= $request->param('limit');
        $fiter_ifdeal=$request->param('fiter_ifdeal');

        if(isset($fiter_ifdeal) && !empty($fiter_ifdeal)){

            $info=Db::name('deal')
                ->alias('a')
                ->join('idle_item b','a.deal_idle_item_id = b.idle_item_id')
                ->join('user c','a.deal_from_user_id = c.user_id')
                ->join('user d','a.deal_to_user_id = d.user_id')
                ->where('b.idle_item_ifdeal',$fiter_ifdeal)
                ->field('b.idle_item_ifdeal,b.idle_item_name,b.idle_item_info,b.idle_item_price,b.idle_item_category,b.idle_item_subject,b.idle_item_img,
                          b.idle_item_degree,b.idle_item_check,b.idle_item_time,c.user_name as deal_from_user_name,c.user_id as deal_from_user_id,
                          a.deal_to_user_remark,a.deal_time,d.user_name as deal_to_user_name')
                ->page($page)->limit($limit)
                ->select();

            $total=$deal->table('deal a,idle_item b')
                        ->where('a.deal_idle_item_id = b.idle_item_id')
                        ->where('b.idle_item_ifdeal',$fiter_ifdeal)->count('deal_id');

        }
        else{
            $info=Db::name('deal')
                ->alias('a')
                ->join('idle_item b','a.deal_idle_item_id = b.idle_item_id')
                ->join('user c','a.deal_from_user_id = c.user_id')
                ->join('user d','a.deal_to_user_id = d.user_id')
                ->field('b.idle_item_ifdeal,b.idle_item_name,b.idle_item_info,b.idle_item_price,b.idle_item_category,b.idle_item_subject,b.idle_item_img,
                          b.idle_item_degree,b.idle_item_check,b.idle_item_time,c.user_name as deal_from_user_name,c.user_id as deal_from_user_id,
                          a.deal_to_user_remark,a.deal_time,d.user_name as deal_to_user_name')
                ->page($page)->limit($limit)
                ->select();

            $total=$deal->count('deal_id');
        }



        return json(['code'=>1,'info'=>$info,'total'=>$total]);

    }



//    买家确认已收到，完成交易
    public function finishDealByBuyer(Request $request){

        $user_id=UserModel::getUseridByToken($request->param('token')); //获取卖家用户id
        $idle_item_id=$request->param('idle_item_id');

        $idle=IdleModel::find($idle_item_id);

        $data=['idle_item_ifdeal' => 2];

        if($idle->save($data)){
            return json(['code'=>1]);
        }

        return json(['code'=>0]);

    }

//    买家、卖家取消交易
    public function cancelDeal(Request $request){

        $user_id=UserModel::getUseridByToken($request->param('token')); //获取卖家用户id
        $idle_item_id=$request->param('idle_item_id');
        $deal_id=$request->param('deal_id');

        $idle=IdleModel::find($idle_item_id);

        $data=['idle_item_ifdeal' => 3];

        $deal=DealModel::find($deal_id);


        if($idle->save($data) && $deal->delete()){
            return json(['code'=>1]);
        }

        return json(['code'=>0]);

    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
